package marvin.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.management.modelmbean.ModelMBean;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JRadioButton;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;

import marvin.dao.MemberDAO;
import marvin.model.MemberVO;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

public class UpdateMember extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField name;
	private JPasswordField pwd;
	private JTextField uid;
	private JPasswordField repwd;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UpdateMember dialog = new UpdateMember("0",0, null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	//public UpdateMember() {
	public UpdateMember( final String mno, final int r, final JTable table ) {
		// 회원 정보 수정을 위해
		// ManageMember에서 전달한 mno를 받기 위해
		// UpdateMember의 기본 생성자 대신
		// mno를 매개변수로 하는 새로운 생성자 작성
		
		// 또한, 회원정보 수정후 ManageMember의
		// JTable의 model도 변경해야 하므로 
		// 수정해야 할 행 row와 JTable 객체도 
		// 생성자 매개변수로 선언
		
		setTitle(":: 회원 수정 ::");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
		
		JLabel txtName = new JLabel("이름");
		txtName.setFont(new Font("굴림", Font.BOLD, 12));
		
		JLabel txtPwd = new JLabel("비밀번호");
		txtPwd.setFont(new Font("굴림", Font.BOLD, 12));
		
		JLabel txtGender = new JLabel("성별");
		txtGender.setFont(new Font("굴림", Font.BOLD, 12));
		
		JLabel txtUid = new JLabel("아이디");
		txtUid.setFont(new Font("굴림", Font.BOLD, 12));
		
		name = new JTextField();
		name.setColumns(10);
		
		JRadioButton isMale = new JRadioButton("남자");
		isMale.setSelected(true);
		isMale.setFont(new Font("나눔고딕코딩", Font.PLAIN, 12));
		
		JRadioButton isFemale = new JRadioButton("여자");
		isFemale.setFont(new Font("나눔고딕코딩", Font.PLAIN, 12));
		
		pwd = new JPasswordField();
		
		uid = new JTextField();
		uid.setEnabled(false);
		uid.setColumns(10);
		
		JLabel txtRepwd = new JLabel("비밀번호확인");
		txtRepwd.setFont(new Font("굴림", Font.BOLD, 12));
		
		repwd = new JPasswordField();
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(txtName)
						.addComponent(txtPwd)
						.addComponent(txtUid)
						.addComponent(txtGender)
						.addComponent(txtRepwd))
					.addGap(53)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(repwd, GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(isMale, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
							.addComponent(isFemale))
						.addGroup(gl_panel.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(uid))
						.addComponent(name, GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
						.addComponent(pwd))
					.addGap(64))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(21)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtName)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(10)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtUid)
						.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtPwd))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtRepwd)
						.addComponent(repwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(10)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtGender)
						.addComponent(isMale)
						.addComponent(isFemale))
					.addContainerGap(38, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 385, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(27, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
					.addContainerGap())
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okBtn = new JButton("수정완료");
				okBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						// 수정완료 버튼 클릭시
						// 수정할 값을 MemberVO에 저장
						MemberVO m = new MemberVO(mno, null, new String( pwd.getPassword() )
													, name.getText(), null, null);
						
						if ( ! Arrays.equals(pwd.getPassword(), repwd.getPassword())){
							JOptionPane.showMessageDialog(null, "비밀번호가 일치하지 않슴돠!");}
						
						else						
						// MemberDAO 의 updateMember 메서드 호출후
						// 실행 결과값을 cnt에 저장
						{int cnt = new MemberDAO().updateMember(m);
						// 회원정보가 제대로 수정된 경우
						// 수정완료 메세지 창 출력
						if(cnt > 0)
							JOptionPane.showMessageDialog(null, "수정완료!");
							// UpdateMember에서 수정된 값을 
							// ManegeMember의 JTable에도 적용해야 함
							// 이름 값만 변경됨 
							table.setValueAt(name.getText(), r, 2);
							table.repaint();
							
						
							dispose(); // 현재창닫기
							// JTable
							
							
						}
					}
				});
				okBtn.setActionCommand("OK");
				buttonPane.add(okBtn);
				getRootPane().setDefaultButton(okBtn);
			}
			{
				JButton notBtn = new JButton("수정취소");
				notBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
						// 수정취소 버튼 클릭시
						// 회원정보 수정 창 닫음
					}
				});
				notBtn.setActionCommand("Cancel");
				buttonPane.add(notBtn);
			}
		}
		
		// 회원번호를 이용해서 특정회원의 모든 정보를 조회
		MemberVO m = new MemberDAO().selectOneMember(mno); 
		// MemberVO의 값을 컴퍼넌트에 출력
		uid.setText( m.getUserid() );
		name.setText( m.getName() );
		pwd.setText( m.getPwd() );
		repwd.setText( m.getPwd() );
		
		
	}// 생성자
}
